package day26;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//反射调用方法
public class Demo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class c=Class.forName("day26.Person");
        Method m1=c.getDeclaredMethod("study");
        Method m2=c.getDeclaredMethod("paly", String.class);
        Method m3=c.getDeclaredMethod("play", String.class, String.class);
        Person p=new Person();
        //m1.invoke(对象，方法实参);  用于调用方法的
        //注意：调用方法的时候，实参一定要和上面定义的形参是匹配的
        m3.setAccessible(true);
        Object result1=m1.invoke(p);
        Object result2=m2.invoke(p,"电脑");
        Object result3=m3.invoke(p,"张三","爱坤");
        System.out.println(result1);
        System.out.println(result2);
        System.out.println(result3);
    }
}
